package cn.bailin233.system.component;

import cn.bailin233.web.utils.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

@ControllerAdvice
public class GlobalExceptionHandler {
    // 自定义异常
    @ExceptionHandler(value = SQLException.class)
    @ResponseBody
    public ResultUtil baseErrorHandler(SQLException e){
        System.out.println("自定义异常");
        return ResultUtil.error(e.getMessage());
    }

    // 未知异常的获取
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultUtil exceptionErrorHandler(Exception e){
        System.out.println(
                        "==================================" +
                        "系统异常" +
                        "==================================" +
                        e.getMessage());
        System.out.println("============================================================================");
        return ResultUtil.error("系统未知错误！");
    }
}
